package fd

import (
	"fmt"
	"github.com/gin-gonic/gin"
	"github.com/jinzhu/gorm"
	"gm/app/model/model"
	"time"
)

//自动分单功能开发 入队
type Seatqueueee struct{
	Name string
	Id int
}
func Enqueue(c *gin.Context){

	//id, _ := strconv.Atoi(c.Query("id"))
	//获取新订单
	orderlist, err := model.FdGetNewOrders()
	if err != nil {
		panic(err)
		return
	}

	if len(orderlist) == 0{
		c.JSON(200, gin.H{
			"code":      200,
			"data":      orderlist,
			"msg":       "暂无新订单",
			"timestamp": time.Now().Unix(),
		})
		return
		}
		//区分订单类型
	groupOrders := model.GroupOrders(orderlist)
	//1、失效、驳回直接挂起到原处理人
	model.DispatchGiveback(groupOrders)
	//3、正常订单入队
	model.DispatchQueue(groupOrders["pdat"]);

	fmt.Println(groupOrders)
	db, _ := gorm.Open("mysql", "root:123456@/dev?charset=utf8&parseTime=True&loc=Local")
	db.SingularTable(true)

	rows,_ :=db.Table("seatqueue").Select("name,id").Rows()

	var archives []*Seatqueueee
	defer rows.Close()
	for rows.Next() {
		var seatqueueee Seatqueueee
		var name string
		rows.Scan(&name)
		seatqueueee.Name = "12312312"
		archives = append(archives, &seatqueueee)
	}


	c.JSON(200, gin.H{
		"code":      200,
		"data":      archives,
		"msg":       "success",
		"timestamp": time.Now().Unix(),
	})
}

/**
 * 自动分单（初审）
 * 出队
 */
func Dequeue(c *gin.Context){


	//取得队列redis
	orderQueue := model.GetOrderQueue()
	orderQueueCount:=len(orderQueue)
	fmt.Println(orderQueueCount)
	seatQueue := model.GetSeatQueue()

	//执行具体分配逻辑还没谢安

	//if orderQueue=="" && orderQueue==""{
	//	break
	//}
	//fmt.Println(orderQueue)
	c.JSON(200, gin.H{
		"code":      200,
		"data":      seatQueue,
		"msg":       "success",
		"timestamp": time.Now().Unix(),
	})

}


/**
 * 测试协成项目
 *
 */
func Xc(c *gin.Context){
	//取得队列redis
	orderQueue := model.GetOrderQueue()
	orderQueueCount:=len(orderQueue)
	fmt.Println(orderQueueCount)
	seatQueue := model.GetSeatQueue()

	//执行具体分配逻辑还没谢安

	//if orderQueue=="" && orderQueue==""{
	//	break
	//}
	//fmt.Println(orderQueue)
	c.JSON(200, gin.H{
		"code":      200,
		"data":      seatQueue,
		"msg":       "success",
		"timestamp": time.Now().Unix(),
	})

}




